<template>
  <div class="product-add" v-loading="loading">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="100px" v-if="!loading">
      <!--基础信息-->
      <Basic></Basic>
      <!--拼团设置-->
      <Recommend></Recommend>
      <div class="common-button-wrapper">
        <el-button size="small" type="info" @click="back">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" :disabled="save_loading">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import PorductApi from '@/api/product.js';
import AssembleApi from '@/api/assemble.js';
import Basic from './part/PrdBasic.vue';
import Recommend from './part/PrdRecommend.vue';
import { formatModel } from '@/utils/base.js';
export default {

  components: {
    /*基础信息*/
    Basic,
    /*规格信息*/
    Recommend
  },
  data() {

    return {
      /*商品ID*/
      assemble_product_id: 0,
      /*判断是编辑*/
      scene: 'edit',
      /*是否正在加载*/
      loading: true,
      /*是否正在提交保存*/
      save_loading: false,
      /*form表单数据*/
      form: {
        model: {},
        spec_list:[],
        /*商品分类*/
        category: [],
        /*运费模板*/
        delivery: [],
        /*会员等级*/
        gradeList: [],
        /*规格数据*/
        specData: null,
        /*是否锁住*/
        isSpecLocked: false,
        /*分销基础设置*/
        basicSetting: {},
        /*分销佣金设置*/
        agentSetting: {}
      },
      /*模型数据*/
      model: {
        /*商品名称*/
        product_name: '',
        /*商品分类*/
        category_id: null,
        /*商品图片*/
        image: [],
        /*商品卖点*/
        selling_point: '',
        /*规格类别,默认10单规格，20多规格*/
        spec_type: 10,
        /*库存计算方式,默认20付款减库存，10下单减库存*/
        deduct_stock_type: 20,
        /*检查用户等级*/
        is_alone_grade: 0,
        
        /*单规格*/
        sku: {
          /*供应商价格*/
          supplier_price: 0,
          /*产品价格*/
          product_price: 0,
          /*产品划线价*/
          line_price: 0,
          /*库存数量*/
          stock_num: 0,
          /*商品重量*/
          product_weight: 0,
          /*商品编码*/
          product_no: '',
        },
        /*限购数量*/
        limit_num: 0,
        audit_status: '',
        audit_remark: ''
      }
    };
  },
  provide: function() {
    return {
      form: this.form
    };
  },
  created() {
    /*获取列表*/
    this.assemble_product_id = this.$route.query.assemble_product_id;
    this.scene = this.$route.query.scene;
    this.getData();
  },
  methods: {
    /**
     * 获取基础数据
     */
    getData: function() {
      let that = this;
      AssembleApi.editProduct(
        {
          assemble_product_id: that.assemble_product_id,
          scene: that.scene
        },
        true
      )
        .then(res => {
          console.log(res.data);
          /*单独产品折扣*/
          let product = res.data.detail;
          
          Object.assign(that.form.model, product);
          that.form.model.spec_type = product.product.spec_type;
          that.form.spec_list = product.assembleSku;
          // console.log( that.form.spec_list  )
          that.loading = false;
        })
        .catch(error => {
          that.loading = false;
        });
    },

    /*转JSON字符串*/
    convertJson(list){
      let obj={};
      list.forEach(item=>{
        obj[item.grade_id]=item.product_equity;
      });
      return JSON.stringify(obj);
    },

    /*提交*/
    onSubmit: function() {
      let self = this;
      this.saveProduct();
    },

    /*图片数值只保留id*/
    ImgKeepId(list) {
      let arr = [];
      for (let i = 0, length = list.length; i < length; i++) {
        let obj = {
          image_id: list[i].image_id,
          file_id: list[i].file_id
        };
        arr.push(obj);
      }
      return arr;
    },

    /*保存为草稿*/
    Draft() {
      let self = this;
      self.form.model.product_status = 30;
      self.onSubmit();
    },

    /*取消*/
    back() {
      this.$router.back(-1);
    },
    getAssembleProduct(){
      let product = this.form.model;
      delete product.assembleSku;
      delete product.product;
      return product;
    },
    //活动ID
    getActivity_id(){
      return this.form.model.assemble_activity_id;
    },
    //保存拼团商品
    saveProduct(){
      let that = this;
      let product = this.getAssembleProduct();
      AssembleApi.recommend({
              product: product
            }, true)
            .then(data => {
              that.save_loading = false;
              if (data.code == 1) {
                that.save_loading = false;
                that.$message({
                  message: '提交成功',
                  type: 'success'
                });
                that.back();

              } else {
                self.$message.error('错了哦，这是一条错误消息');
              }
            })
            .catch(error => {
              that.save_loading = false;
            });
    },
  }
};
</script>

<style lang="scss" scoped>
.product-add {
  padding-bottom: 100px;
}
.mb50{
  margin-bottom: 50px;
}

</style>
